package com.micro.mq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Service;


/**
 * 延时消费示例
 *
 * @author pan.shun
 * @since 2022/7/19 15:42
 */
@Slf4j
@Service
public class KafkaConsumer {

    /**
     * <p>
     * 这里的sendTo 是将消息内容处理后交至另一个topic里面去
     */
    @SendTo("foo3")
    @KafkaListener(topics = "${app.topic.foo}", groupId = "${spring.kafka.consumer.group-id}")
    public String receive2(ConsumerRecord<?, ?> cr, Acknowledgment ack) {
        Object value = cr.value();
        log.info("初始数据为：{}", value);
        ack.acknowledge();
        return "加工后的数据< " + value + ">";
    }


    @KafkaListener(topics = "${app.topic.foo3}", groupId = "${spring.kafka.consumer.group-id}")
    public void onMessage(ConsumerRecord<?, ?> cr, Acknowledgment ack
    ) {
        log.info("延时消息为= {} ", cr.value());
        ack.acknowledge();
    }


}
